<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="68px">
          <el-form-item label="创建时间" style="width: 308px">
            <el-date-picker
              v-model="dateRange"
              value-format="x"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="handleQuery" icon="Search">搜索</el-button>
            <el-button @click="resetQuery" icon="Refresh">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['system:role:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
        </el-row>
      </template>

      <el-table ref="roleTableRef" v-loading="loading" :data="recordList">
        <el-table-column label="设备名称" prop="deviceName" width="400" />
        <el-table-column label="人员工号" prop="employeeNo" width="200" />
        <el-table-column label="人员姓名" prop="employeeName" width="200" />
        <el-table-column label="人员类型" prop="userType" width="200">
          <template #default="scope">
            <span>{{ parseUserTypeDesc(scope.row.userType) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="开门时间" align="center" prop="dateTime">
          <template #default="scope">
            <span>{{ parseTime(scope.row.dateTime) }}</span>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script lang="ts" setup>

import { ComponentInternalInstance } from 'vue'
import { ElTree, DateModelType, FormInstance } from 'element-plus'
import {getDoorAccessReporList, DoorAccessRecordVO, DoorAccessRecordQuery,DoorAccessRecordForm} from '../api/doorAccessReport.api'




const router = useRouter()
const { proxy } = getCurrentInstance() as ComponentInternalInstance
const showSearch = ref(true)
const dateRange = ref<[DateModelType, DateModelType]>(['', ''])
const queryFormRef = ref<FormInstance>()
const useTypes = ref(
  [
    {id: 'normal', name: '普通人（主人）'},
    {id: 'visitor', name: '来宾（访客）'},
    {id: 'blackList', name: '非授权名单人'},
    {id: 'administrators', name: '管理员'},
    {id: 'operator', name: '操作员'}
  ])
// userType--人员类型 [normal#普通人（主人）,visitor#来宾（访客）,blackList#非授权名单人,administrators#管理员,operator#操作员]

const data = reactive<PageData<DoorAccessRecordForm, DoorAccessRecordQuery>>({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    dateTimeBegin: null,
    dateTimeEnd: null,

  },
  rules: {
    roleName: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
    roleKey: [{ required: true, message: '权限字符不能为空', trigger: 'blur' }],
    roleSort: [{ required: true, message: '角色顺序不能为空', trigger: 'blur' }],
  },
})
const { form, queryParams, rules } = toRefs(data)
const loading = ref(false)
const recordList = ref<DoorAccessRecordVO[]>()
const total = ref(0)
/**
 * 搜索按钮操作
 */
const handleQuery = () => {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置 */
const resetQuery = () => {
  dateRange.value = ['', '']
  queryFormRef.value?.resetFields()
  handleQuery()
}

const getList = () => {
  loading.value = true
  console.info(dateRange)
  queryParams.value.dateTimeBegin=dateRange.value[0]
  queryParams.value.dateTimeEnd=dateRange.value[1]
  console.info(queryParams.value)
  getDoorAccessReporList(queryParams.value).then((res: any) => {
    recordList.value = res.data.rows
    total.value = res.data.total
    loading.value = false
  })
}
const parseUserTypeDesc=(ut)=>{
  return useTypes.value.find(x => { return  x.id == ut})?.name || ''
}

/** 导出按钮操作 */
const handleExport = () => {
  console.info({dateTimeBegin: dateRange.value[0], dateTimeEnd: dateRange.value[1]})
  proxy?.download(
    'report/doorAccessReport/export',
    JSON.stringify( {dateTimeBegin: dateRange.value[0], dateTimeEnd: dateRange.value[1]}),
    `门禁记录_${new Date().getTime()}.xlsx`
  )
}
onMounted(() => {
  getList()
})

// getData()
</script>

<!-- <style lang="scss" scoped>

</style> -->
